//
//  XTGoodsViewController.m
//  BeautifulHome
//
//  Created by dlios on 15-5-14.
//  Copyright (c) 2015年 ACE. All rights reserved.
//

#import "XTGoodsViewController.h"
#import "UIImageView+WebCache.h"
#import "XTGoods.h"

@interface XTGoodsViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, retain) UICollectionView *myCollectionView;
@property (nonatomic, retain) UILabel *shopName;
@property (nonatomic, retain) UILabel *price;
@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, retain) UIAlertView *alertView;

@end

@implementation XTGoodsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"商品";
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStyleDone target:self action:@selector(rightButtonAction)];
    
    [self createdView];
}
- (void)createdView
{
    UILabel *mylabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20, 100, 25)];
    mylabel.text = @"商品详情";
    [self.view addSubview:mylabel];
    [mylabel release];
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height / 2 - 100);
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, -42, self.view.frame.size.width, self.view.frame.size.height / 2) collectionViewLayout:flowLayout];
//    _myCollectionView.pagingEnabled = YES;
    _myCollectionView.dataSource = self;
    _myCollectionView.delegate = self;
    _myCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_myCollectionView];
    [_myCollectionView release];
    [_myCollectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"my"];
    self.shopName = [[UILabel alloc] initWithFrame:CGRectMake(10, self.view.frame.size.height / 2 - 20,  275, 35)];
    _shopName.text = self.myTitle;
    _shopName.numberOfLines = 0;
    [_shopName sizeToFit];
    [self.view addSubview:_shopName];
    [_shopName release];
    self.price = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 90, self.view.frame.size.height / 2, 90, 35)];
    if ([self.prices isEqualToString:@"0.00"]) {
        _price.text = @"暂无参考";
    } else if (self.prices == nil) {
        _price.text = @"暂无参考";
    } else {
        _price.text = self.prices;
    }
    
    NSLog(@"!!!%@, %@", _price.text, self.prices);
    [self.view addSubview:_price];
    [_price release];
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height / 2 + 64, self.view.frame.size.width, 200)style:UITableViewStyleGrouped];
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    _myTableView.rowHeight = 80;
    _myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_myTableView];
    [_myTableView release];
    
    
    NSLog(@"..........%@", self.myGoods.addresses);
    NSLog(@"..........%@", self.myGoods.price);
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.address == nil) {
        return 1;
    }
    else {
        return self.address.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"str";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
    }
    if (self.address == nil) {
        cell.textLabel.text = @"暂无卖家地址与详细信息";
    }
    else {
        cell.textLabel.text = [self.address[indexPath.row] objectForKey:@"title"];
        cell.detailTextLabel.text = [self.address[indexPath.row] objectForKey:@"address"];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _imageUrl.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *resue = @"my";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:resue forIndexPath:indexPath];
    UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 353 - 80)] autorelease];
    [imageView sd_setImageWithURL:[NSURL URLWithString:self.imageUrl[indexPath.row]]placeholderImage:[UIImage imageNamed:@"3-1"]];
    for (id subView in cell.contentView.subviews) {
        [subView removeFromSuperview];
    }
    [cell.contentView addSubview:imageView];
    return cell;
}
- (void)rightButtonAction
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    //创建文件管理器(单例)
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //添加文件夹
    //拼接文件夹地址
    NSString *filePath = [path stringByAppendingPathComponent:@"收藏"];
    //创建文件夹
    [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *strPath = [filePath stringByAppendingPathComponent:@"mygoods.ppa"];
    if (![fileManager fileExistsAtPath:strPath]) {
        NSMutableArray *arr = [NSMutableArray array];
        [arr addObject:self.myGoods];
        [NSKeyedArchiver archiveRootObject:arr toFile:strPath];
        self.alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(dismissAlertView) userInfo:nil repeats:NO];
        [_alertView show];
    } else {
        NSMutableArray *arr = [NSKeyedUnarchiver unarchiveObjectWithFile:strPath];
        NSMutableArray *newArr = [NSMutableArray array];
        for (XTGoods *XTMygoods in arr) {
            [newArr addObject: XTMygoods.title];
        }
        if (![newArr containsObject:self.myGoods.title]) {
            [arr addObject:self.myGoods];
            [NSKeyedArchiver archiveRootObject:arr toFile:strPath];
            self.alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(dismissAlertView) userInfo:nil repeats:NO];
            [_alertView show];
        } else {
            self.alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"已收藏" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
            [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(dismissAlertView) userInfo:nil repeats:NO];
            [_alertView show];
        }
    }
}
- (void)dismissAlertView
{
    [_alertView dismissWithClickedButtonIndex:0 animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
